package org.wevil.controller.news;

import org.springframework.web.bind.annotation.*;
import org.wevil.service.SubjectService;
import org.wevil.util.JwtUtil;
import org.wevil.util.NodeUtil;
import org.wevil.vo.Result;
import org.wevil.vo.list.SubjectListVo;
import org.wevil.vo.node.NodeVo;
import org.wevil.vo.node.SubjectNodeVo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/** 栏目管理 */
@RestController
@RequestMapping("subject")
public class SubjectController {

    @Resource
    private SubjectService subjectService;

    /** 获取栏目列表。根据角色返回数据 */
    @PostMapping("/list")
    public Result<List<SubjectListVo>> list(
            @RequestHeader("token") String token
    ){
        Integer userId = JwtUtil.getUserId(token);
        List<SubjectListVo> data = subjectService.userList(userId);
        return Result.success(data);
    }

    /** 栏目树形菜单结构数据。根据角色返回数据 */
    @GetMapping("/node")
    public Result<List<? extends NodeVo>> node(
            @RequestHeader("token") String token
    ) {
        Integer userId = JwtUtil.getUserId(token);
        List<SubjectNodeVo> menuNodeVos = subjectService.userNode(userId);
        List<? extends NodeVo> data = NodeUtil.listWithTree(menuNodeVos);
        return Result.success(data);
    }

    /** 获取子栏目列表数据。包含递归到最下层 */
    @GetMapping("/subList/{id}")
    public Result<List<SubjectListVo>> subList(
             @PathVariable Integer id
    ) {
        List<SubjectListVo> data = subjectService.getSubById(id);
        return Result.success(data);
    }

    /** 获取子栏目节点数据。包含递归到最下层 */
    @PostMapping("/subNodeList/{id}")
    public Result<List<? extends NodeVo>> subNodeList(
             @PathVariable Integer id
    ) {
        List<SubjectNodeVo> menuNodeVos = subjectService.getSubNodeById(id);
        List<? extends NodeVo> data = new ArrayList<>();
        if(menuNodeVos!=null && menuNodeVos.size()>0){
            data = NodeUtil.listWithTree(menuNodeVos);
        }
        return Result.success(data);
    }
}
